args <- commandArgs(TRUE)
setwd(args[1])
library(survival)
library(penalized)
expr<-read.table("temp_input",sep="\t",head=T)
#expr<-read.table("input_expr",sep="\t",head=T)
ref<-read.table("/data1/bsi/BORA_processing/devel/eqtl/parallelize_genotypingsurvival/result_ref_file",sep="\t")
rownames(ref)<-ref[,1]
expr1<-merge(ref,expr,by="row.names",all.x=F)
geno<-read.table("/data1/bsi/BORA_processing/devel/eqtl/pinalized/input_withcorrecthead.tped",sep=" ",head=T)
rownames(geno)<-geno[,2]
#martingale order
ordersamples<-read.table("/data1/bsi/BORA_processing/devel/eqtl/parallelize_genotypingsurvival/ordered_sampes_Martingale.txt")
ordersamples<-as.vector(ordersamples[,1])
ordersamples<-gsub("-",".",ordersamples)
#martingale order

i<-1
#clin<-read.delim("/data1/bsi/BORA_processing/devel/eqtl/parallelize_genotypingsurvival/clinical" ,sep="\t",quote="\"",dec=".",fill=TRUE,comment.char="",header=TRUE,col.names=c("SUBJECT","AGE","GENDER","OS","PFS","DFS","ALIVEOS","ALIVEPFS","ALIVEDFS","SITE"),as.is=c(TRUE,TRUE,FALSE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,FALSE),colClasses=c("character","numeric","factor","numeric","numeric","numeric","integer","integer","integer","factor"))
clin<-read.delim("/data1/bsi/BORA_processing/devel/eqtl/pinalized/clinical" ,sep="\t",quote="\"",dec=".",fill=TRUE,comment.char="",header=TRUE,col.names=c("SUBJECT","AGE","GENDER","OS","PFS","DFS","ALIVEOS","ALIVEPFS","ALIVEDFS","SITE","ClVerhaakPub"),as.is=c(TRUE,TRUE,FALSE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,FALSE,TRUE),colClasses=c("character","numeric","factor","numeric","numeric","numeric","integer","integer","integer","factor","factor"))
clin$SUBJECT<-gsub("-",".",clin$SUBJECT)
AUCrank<-c()
rownames(expr1)<- expr1[,1]
rownames(clin)<-clin[,1]

nn<-ncol(clin)+4
while(i<=ncol(expr1))
{
	gg<-t(expr1[i,])
	rownames(gg)<-colnames(expr1)
	colnames(gg)<-c("gene")
	l1<-expr1[i,4]-20000
	l2<-expr1[i,4]+20000
	l3<-expr1[i,3]
	index<-c()
	index<-which(geno[,4]>=l1 & geno[,4]<l2  & geno[,1] == l3)
	if(length(index) > 0)
	{
		geno1<-geno[index,]
		geno2<-t(geno1)
		rownames(geno2)<-colnames(geno1)
		colnames(geno2)<-rownames(geno1)
		rownames(clin)<-clin[,1]
		merge1<-merge(clin,geno2,by="row.names",all.x=F)
		rownames(merge1)<-merge1[,1]
		merge2<-merge(gg,merge1,by="row.names",all.x=F)
		colnames_merge2<-colnames(merge2)
		vec<-paste(colnames_merge2[nn:length(colnames_merge2)], collapse = "+")
		#merge2$gene<-as.numeric(merge2$gene)
		ku<-merge2$gene
		ku<-as.numeric(levels(ku)[ku])
		merge2$gene<-ku
		kunum<-nn
		while(kunum<= ncol(merge2))
		{
			ku<-merge2[,kunum]
			ku<-as.numeric(levels(ku)[ku])
			merge2[,kunum]<-ku
			kunum=kunum+1
		}

		j=1
		order_samp<-c()
		while(j<length(ordersamples))
		{
			order_samp<-c(order_samp,which(merge2[,1]==ordersamples[j]))
			j<-j+1
		}
		order_samp<-c(order_samp,which(merge2[,1]==ordersamples[length(ordersamples)]))
		 merge2<-merge2[order_samp,]
		#merge2[,nn:ncol(merge2)]<-as.numeric(merge2[,nn:ncol(merge2)])
		#K<-parse(text=paste("Surv(OS,ALIVEOS)~gene",paste(colnames_merge2[nn:length(colnames_merge2)], collapse = "+"),sep="+"))
		#optL2 (Surv(OS,ALIVEOS)~gene+rs12548816, unpenalized=merge2[,5],data=merge2,model ="cox")
		#optL1 (Surv(OS,ALIVEOS)~gene+rs12548816, unpenalized=merge2[,5],data=merge2,model ="cox",minlambda1=29.01398, maxlambda1=725.3495)
		#fit<-cvl(Surv(OS,ALIVEOS)~gene+rs12548816, unpenalized=merge2[,5],data=merge2,model ="cox",lambda1=294.9905,fold=20)
		#opt2<-optL2 (eval(K), unpenalized=merge2[,5],data=merge2,model ="cox")
		K<-parse(text=paste("~gene",paste(colnames_merge2[nn:length(colnames_merge2)], collapse = "+"),sep="+"))
		opt2<-optL2(Surv(OS,ALIVEOS), penalized=eval(K),unpenalized=~AGE,data=merge2,model ="cox")
		lambda2<-opt2$lambda
		if(lambda2<= 0.0001) {lambda2<-0}
		opt1<-optL1 (eval(K), unpenalized=merge2[,5],data=merge2,model ="cox",minlambda1=0.0001,lambda2=lambda2)
		lambda1<-opt1$lambda
		if(lambda1<= 0.0001) {lambda1<-0}
		#create fold sampes
			d<-1
			foldsamples<-seq(1,nrow(merge2))
			while(d<num+1)
			{			
				s<-d;
				sub<-c()
				main<-c()
				if(d > 1)
				{
					main<-seq(1,d-1,1)
			}
		nu<-1
		while(s<nrow(merge2)+1)
		{
			sub[nu]<-s
			sequ<-c()		
			if(s+1 <= length(geno1))
			{	
				sequ<-seq(s+1,s+num-1,1)
				if(s+num-1 > length(geno1))
				{
					sequ<-seq(s+1,length(geno1),1)
				}
			}
			main<-c(main,sequ)
			nu<-nu+1	
			s<-s+num
		}
		foldsamples[sub] =rep(d,length(sub))
		d<-d+1
	}
		#end fold samples
		fit<-cvl(Surv(OS,ALIVEOS)~gene+rs12548816, unpenalized=merge2[,5],data=merge2,model ="cox",lambda1=lambda1,lambda2=lambda2,fold=20)
		Survival_probs<-survival(fit$predictions,median(merge2$OS))
#		order_rank<-order(Survival_probs)
#		sorted_ranks<-seq(1,length(order_rank),1)
#		order_of_index<-order(order_rank)
#		wanted_ranks<-length(order_of_index)+1-sorted_ranks[order_of_index]
		wanted_ranks<-length(Survival_probs)+1-rank(Survival_probs)
		wr1<-matrix(nrow=1, ncol=length(wanted_ranks))
		wr1[1,]<-wanted_ranks
		colnames(wr1)<-merge2[,1]
		
		dt <- merge(clin,t(wr1),by.y="row.names",by.x="SUBJECT")
		colnames(dt) <- c("SUBJECT","AGE","GENDER","OS","PFS","DFS","ALIVEOS","ALIVEPFS","ALIVEDFS","SITE","CAL","RANK")
		j=1
		order_samp<-c()
		while(j<length(ordersamples))
		{
			order_samp<-c(order_samp,which(dt[,1]==ordersamples[j]))
			j<-j+1
		}
		order_samp<-c(order_samp,which(dt[,1]==ordersamples[length(ordersamples)]))
		dt<-dt[order_samp,]
		surv<-survConcordance(Surv(OS,ALIVEOS)~RANK,data=dt)
		AUC<-surv$concordance
		AUC1<-c()
		AUC1<-c(expr1[i,1],AUC)
		AUCrank<-rbind(AUCrank,AUC1)
	}
	i<-i+1
	
}
colnames(AUCrank)<-c("GENE","AUC")
write.table(AUCrank,"AUC.txt",quote=FALSE,sep="\t",col.names=TRUE,row.names=FALSE)
